package hylanda;

import java.lang.*;
import java.io.*;
import java.util.*;
import hylanda.dayooclient.*;

import java.lang.*;
import java.io.*;
import java.util.*;
import hylanda.dayooclient.*;

/**
 * 此处插入类型说明。 创建日期：(2007-3-27 15:56:05)
 * 
 * @author：guoran
 */

public class dayooclient_test {
	public final String CMD_AUTOTEST = "auto"; // 自动测试
	public final String CMD_EXIT = "exit"; // 退出
	public final String CMD_HELP = "help"; // 帮助
	public final String HL_PROMPT_CHAR = "<hldayootest>$ ";
	public FileWriter fw;
	public BufferedWriter bw;
	// public Long lTotalTime;
	public int nTotalTime;
	public int nTotalLen;
	public int nTotalCnt;

	private DayooClient m_DayooClient;

	public static void main(String[] args) {
		dayooclient_test test = new dayooclient_test();
		test.run();
	}

	public void run() {
		try {
			fw = new FileWriter("result.txt");
			bw = new BufferedWriter(fw);
			bw.write("编号\t耗时\t长度\t路径\r\n");
			bw.close();
			fw.close();
		} catch (Exception e) {
			System.out.println("error creat log....");
			e.printStackTrace();
		}

		try {
			fw = new FileWriter("c:\\result.txt");
			bw = new BufferedWriter(fw);
			bw.write("编号\t耗时\t长度\t路径\r\n");
			// lTotalTime = java.lang.Long.getLong("0");
			nTotalTime = 0;
			nTotalLen = 0;
			nTotalCnt = 0;
		} catch (Exception e) {
			System.out.println("error creat log....");
			e.printStackTrace();
		}

		m_DayooClient = new DayooClient();
		System.out.println("大洋网客户端java接口测试\n");

		// 连接服务器ip 
		System.out.print("Input IP:\n");
		String szIp = GetCmd();
		if (szIp.equalsIgnoreCase(""))
			return;
		// 连接服务器port
		System.out.print("Input PORT:\n");
		String szPort = GetCmd();
		if (szPort.equalsIgnoreCase(""))
			return;
		int nPort = Integer.parseInt(szPort);

		// 创建连接对象
		int nConnRet = m_DayooClient.HLDayooConnect(szIp, nPort);

		if (nConnRet == 0) {
			System.out.print("Connect with server error!");
			return;
		}
		while (true) {
			System.out.print("\nplease select test type:\n");
			System.out.print(HL_PROMPT_CHAR);
			String szCommLine = GetCmd();
			String strComm = szCommLine;

			// 退出
			if (strComm.equalsIgnoreCase(CMD_EXIT)) {
				break;
			} else if (strComm.equalsIgnoreCase(CMD_AUTOTEST)) { // 自动

				// 原数据路径
				System.out.print("Input Source Files Path:\n");
				String szSourcePath = GetCmd();
				if (szSourcePath.equalsIgnoreCase(""))
					return;
				// 结果保存路径
				System.out.print("Input Result Save Path:\n");
				String szResultPath = GetCmd();
				// if (szResultPath.equalsIgnoreCase(""))
				// return;
				// 控制命令
				System.out
						.print("Input Cmd (eg:charset=gb2312;abs=1;keyword=1;class=1;AbsLen=250;kwdcnt=10):\n");
				String szCmd = GetCmd();
				if (szCmd.equalsIgnoreCase(""))
					break;

				walkAllFile(nConnRet, szSourcePath, szResultPath, szCmd);
			} else if (strComm.equalsIgnoreCase(CMD_HELP)) { // 帮助
				DoHelp();
			} else {
				System.out.println("No Find Command\n");
			}

		}
		// 释放连接对象
		m_DayooClient.HLDayooDisconnect(nConnRet);
		try {
			bw.write("Total\t" + nTotalTime + "\t"
					+ nTotalLen + "\tendle");
			bw.close();
			fw.close();
		} catch (Exception e) {
			System.out.println("error write log endle....");
			e.printStackTrace();
		}
	}

	public void walkAllFile(int hDayoo, String strSrcDir, String strResDir,
			String strCmd) {
		File cFileSrc = new File(strSrcDir);
		try {
			if (cFileSrc.exists()) {
				File list[] = cFileSrc.listFiles();
				for (int i = 0; i < list.length; i++) {
					if (strResDir.equalsIgnoreCase(""))
						walk(hDayoo, list[i], new File(""), strCmd);
					else {
						if (!strResDir.endsWith("\\"))
							strResDir += "\\";
						String strResDirChild = strResDir + list[i].getName();
						File cFileChild = new File(strResDirChild);
						if (!cFileChild.exists() && list[i].isDirectory()
								&& !cFileChild.mkdir())
							continue;
						walk(hDayoo, list[i], cFileChild, strCmd);
					}
				}
			}
		} catch (Exception e) {
			System.out.println("error walk dir....");
			e.printStackTrace();
		}
	}

	public void walk(int hDayoo, File cFileSrc, File cFileRes, String strCmd) {
		if (cFileSrc.isDirectory()) {
			String list[] = cFileSrc.list();
			for (int i = 0; i < list.length; i++) {
				File cFileSrcChild = new File(cFileSrc, list[i]);
				if (!cFileRes.exists())
					walk(hDayoo, cFileSrcChild, new File(""), strCmd);
				else {
					File cFileResChild = new File(cFileRes, list[i]);
					if (!cFileResChild.exists() && cFileSrcChild.isDirectory()
							&& !cFileResChild.mkdir())
						continue;
					walk(hDayoo, cFileSrcChild, cFileResChild, strCmd);
				}
			}
		} else if (cFileSrc.getName().endsWith(".HTM")
				|| cFileSrc.getName().endsWith(".htm")) {
			HLDayooTest(hDayoo, cFileSrc, cFileRes, strCmd); //
		} else
			return;
	}

	public boolean HLDayooTest(int hDayoo, File cFileSrc, File cFileRes,
			String strCmd) {
		boolean bRet = false;
		String szText = ""; // 置空
		String szPath = cFileSrc.getPath();
		String szResPath = cFileRes.getPath();
		// 分析出文件charset
		String szCharSetName = "";
		szCharSetName = strCmd.substring(strCmd.indexOf('=') + 1, strCmd
				.indexOf(';'));
		try {
			szText = readerToString(szPath, szCharSetName);
		} catch (IOException e) {
			System.out.print("error read file...");
			e.printStackTrace();
			return bRet;
		}

		long lBcur = System.currentTimeMillis();
		System.out.println("-----hDayoo-----\n" + hDayoo);
		System.out.println("-----szText-----\n" + szText);
		System.out.println("-----strCmd-----\n" + strCmd);
		String szTextResult = m_DayooClient.HLDayooAnalyzeAndGetResult(hDayoo,
				szText, strCmd);
		System.out.println("-----szTextResult-----\n" + szTextResult);
		long lEcur = System.currentTimeMillis();
		long lUcur = lEcur - lBcur;
		// lTotalTime += lUcur;
		int nUcur = (int)lUcur;
		nTotalTime += (int)nUcur;
		int nTlen = szText.length();
		nTotalLen += nTlen;
		try {
			bw.write(nTotalCnt + "\t" + nUcur  + "\t"
					+ nTlen + "\t" + szPath + "\r\n");
			bw.flush();
		} catch (IOException e) {
			System.out.print("error write log...");
			e.printStackTrace();
			return bRet;
		}
		try {
			if (szResPath.equalsIgnoreCase("")) {
			} else {
				FileWriter fResFile = new FileWriter(szResPath);
				fResFile.write(szTextResult);
				fResFile.close();
			}
		} catch (IOException e) {
			System.out.print("error write file...");
			e.printStackTrace();
			return bRet;
		}
		bRet = true;
		nTotalCnt++;
		return bRet;
	}

	public String GetCmd() {
		try {
			BufferedReader binSrc = new BufferedReader(new InputStreamReader(
					System.in));
			String strTmp = binSrc.readLine();
			return strTmp;
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		return "";
	}

	// 读取文件的全部内容
	public String readerToString(String szFilePath, String szCharSet)
			throws IOException {
		String strAll = "";
		String strRecord = null;
		try {
			FileInputStream fis = new FileInputStream(szFilePath);
			InputStreamReader ir = new InputStreamReader(fis, szCharSet);
			BufferedReader in = new BufferedReader(ir);

			strRecord = new String();
			while ((strRecord = in.readLine()) != null) {
				strAll += strRecord;
			}
			in.close();
			ir.close();
			fis.close();
		} catch (IOException e) {
			System.out.println("Uh oh, got an IOException error!");
			e.printStackTrace();
		}
		return strAll;
	}

	public void DoHelp() {
		System.out.println("<Command List>");
		System.out.println(CMD_AUTOTEST + " \t\t-- 自动测试");
		System.out.println(CMD_EXIT + " \t\t-- 退出");
		System.out.println(CMD_HELP + " \t\t-- 帮助");
	}
}